package com.zw.mes.module.cost.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.zw.mes.base.BaseController;
import com.zw.mes.common.JsonResult;
import com.zw.mes.module.cost.model.CosSheet;
import com.zw.mes.module.cost.model.DictCommon;
import com.zw.mes.module.cost.model.Project;
import com.zw.mes.module.cost.service.CostService;
import com.zw.mes.module.cost.service.DictCommonService;
import com.zw.mes.module.cost.service.SheetService;
import com.zw.mes.util.ShiroUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zw.mes.constant.Constant.GROUP_SPECTYPE;
import static com.zw.mes.constant.Constant.GROUP_WEIGHTTYPE;


/**
 * @Description: 设计成本业务
 * @Author: China.wsg
 * @Date: 2020/11/10 08:58
 */
@Controller
@RequestMapping("/cost/dsnCost")
@SuppressWarnings("all")
public class DsnCostController extends BaseController {

    @Autowired
    private CostService costService;
    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private SheetService sheetService;

    @GetMapping("/project/list")
    @ResponseBody
    public List<Project> proList(String q) {
        return sheetService.findProjectByCode(q);
    }
    /**
     * 设计工时确认单录入页面
     *
     * @param model
     * @return
     */
    @GetMapping("/create")
    //@RequiresPermissions({"cost:Deshou:view"})
    public String createPage(Model model) {
        model.addAttribute("special", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        model.addAttribute("weightType", dictCommonService.findDictByGroupName(GROUP_WEIGHTTYPE));
        return "cost/dsnCost/createCostPage";
    }

    /**
     * 项目编码存在校验
     * 页面级校验
     *
     * @param param
     * @return
     */
    @PostMapping("/codeExist")
    @ResponseBody
    public String codeExist(String proCode) {
        Map<String, Boolean> map = new HashMap<String, Boolean>();
        map.put("valid", costService.codeExist(proCode));
        String resultString = "";
        ObjectMapper mapper = new ObjectMapper();
        try {
            resultString = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return resultString;
    }

    /**
     * 项目编码存在校验
     * 页面级校验
     *
     * @param param
     * @return
     */
    @GetMapping("/project")
    @ResponseBody
    public List<Project> findProject(String proCode) {
        return costService.findProjectByCode(proCode);
    }

    /**
     * 获取重量类别
     *
     * @return
     */
    @PostMapping("/weightType")
    @ResponseBody
    //@RequiresPermissions(value = {"cost:Deshou:view", "cost:DeshouConf:view"}, logical = Logical.OR)
    public List<DictCommon> weightTypeList() {
        return dictCommonService.findDictByGroupName(GROUP_WEIGHTTYPE);
    }

    /**
     * 保存设计工时确认单
     */
    @ResponseBody
    @PostMapping("/save")
    //@RequiresPermissions({"cost:Deshou:edit"})
    public JsonResult saveCost(CosSheet cosSheet, Double[] hQuantity, String[] hRemark, String[] wDescribe, Double[] wQuantity, String[] wUnit, String[] wRemark) {
        return jsonResult(costService.saveDsnCost(cosSheet, hQuantity, hRemark, wDescribe, wQuantity, wUnit, wRemark));
    }

    /**
     * 设计工时确认单提交页面
     *
     * @return
     */
    @GetMapping("/subList")
    //@RequiresPermissions({"cost:DeshouConf:view"})
    public String subPage() {
        return "cost/dsnCost/subCostPage";
    }

    /**
     * 待提交列表
     */
    @ResponseBody
    @PostMapping("/subList")
    //@RequiresPermissions({"cost:DeshouConf:view"})
    public JsonResult subList(CosSheet cosSheet) {
        startPage();
        cosSheet.setCostType(1);// 设计工时确认单
        cosSheet.setCreateCode(ShiroUtils.getUser().getAccount());
        List<CosSheet> list = costService.findCosSheetByQuery(cosSheet);
        return jsonResult(list);
    }

    /**
     * 修改功能页面
     *
     * @param sheetId
     * @param model
     * @return
     */
    @GetMapping("/edit/{id}")
    //@RequiresPermissions({"cost:DeshouConf:edit"})
    public String editSheet(@PathVariable("id") Integer cosSheetId, Model model) {

        CosSheet cosSheet = costService.getCosSheetById(cosSheetId);
        model.addAttribute("cosSheet", cosSheet);
        model.addAttribute("special", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        model.addAttribute("weightType", dictCommonService.findDictByGroupName(GROUP_WEIGHTTYPE));
        model.addAttribute("proPart", sheetService.findPartByProjectCode(cosSheet.getProCode(), "外观".equals(cosSheet.getSpecial()) ? null : cosSheet.getSpecial()));
        model.addAttribute("hCosDetail", costService.findCosDetailByCosSheetIdAndDataType(cosSheetId, "'人工工时'"));
        model.addAttribute("wCosDetail", costService.findCosDetailByCosSheetIdAndDataType(cosSheetId, "'结构总重','结构净重'"));
        return "cost/dsnCost/editCostPage";
    }

    /**
     * 修改设计工时确认单
     */
    @ResponseBody
    @PostMapping("/edit")
    //@RequiresPermissions({"cost:DeshouConf:edit"})
    public JsonResult editCost(CosSheet cosSheet, Double[] hQuantity, String[] hRemark, String[] wDescribe, Double[] wQuantity, String[] wUnit, String[] wRemark) {
        return jsonResult(costService.editDsnCost(cosSheet, hQuantity, hRemark, wDescribe, wQuantity, wUnit, wRemark));
    }

    /**
     * 查看功能页面
     *
     * @param sheetId
     * @param model
     * @return
     */
    @GetMapping("/view/{id}")
    //@RequiresPermissions(value = {"cost:Deshou:view", "cost:DeshouConf:view"}, logical = Logical.OR)
    public String viewSheet(@PathVariable("id") Integer cosSheetId, Model model) {

        CosSheet cosSheet = costService.getCosSheetById(cosSheetId);
        model.addAttribute("cosSheet", cosSheet);
        model.addAttribute("special", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        model.addAttribute("weightType", dictCommonService.findDictByGroupName(GROUP_WEIGHTTYPE));
        model.addAttribute("proPart", sheetService.findPartByProjectCode(cosSheet.getProCode(), "外观".equals(cosSheet.getSpecial()) ? null : cosSheet.getSpecial()));
        model.addAttribute("hCosDetail", costService.findCosDetailByCosSheetIdAndDataType(cosSheetId, "'人工工时'"));
        model.addAttribute("wCosDetail", costService.findCosDetailByCosSheetIdAndDataType(cosSheetId, "'结构总重','结构净重'"));
        return "cost/dsnCost/viewCostPage";
    }

    /**
     * 删除成本单
     */
    @ResponseBody
    @PostMapping("/del")
    //@RequiresPermissions({"cost:DeshouConf:del"})
    public JsonResult delCost(Integer id) {
        return jsonResult(costService.delCostById(id, "设计工时确认单"));
    }

    /**
     * 提交
     */
    @ResponseBody
    @PostMapping("/submit")
    @RequiresPermissions({"cost:DeshouConf:commit"})
    public JsonResult submitCost(Integer id) {
        return jsonResult(costService.submitCost(id, "设计工时确认单"));
    }

    /**
     * 设计工时确认单审核页面
     *
     * @return
     */
    @GetMapping("/checkList")
    @RequiresPermissions({"cost:Deshouexa:view"})
    public String checkPage() {
        return "cost/dsnCost/checkCostPage";
    }

    /**
     * 待审核列表
     */
    @ResponseBody
    @PostMapping("/checkList")
    //@RequiresPermissions({"cost:Deshouexa:view"})
    public JsonResult checkList(CosSheet cosSheet) {
        startPage();
        cosSheet.setCostType(1);// 设计工时确认单
        cosSheet.setPageQuery(3);// 审核页面
//        cosSheet.setVerifyId(ShiroUtils.getUser().getId());


        List<CosSheet> list = costService.findCosSheetByQuery(cosSheet);
        return jsonResult(list);
    }

    /**
     * 审核
     */
    @ResponseBody
    @PostMapping("/check")
    @RequiresPermissions(value = {"cost:Blanking:success","cost:rivetList:success","cost:heatList:success","cost:assembList:success","cost:PaintList:success","cost:instalList:success","cost:wrapCheck:success"},logical = Logical.OR)
    public JsonResult checkCost(Integer id) {
        return jsonResult(costService.checkCost(id, "设计工时确认单"));
    }

    /**
     * 驳回
     *
     * @param cosSheet
     * @return
     */
    @ResponseBody
    @PostMapping("/reject")
    @RequiresPermissions(value = {"cost:Blanking:success","cost:rivetList:success","cost:heatList:success","cost:assembList:success","cost:PaintList:success","cost:instalList:success","cost:wrapCheck:success"},logical = Logical.OR)
    public JsonResult rejectCost(Integer id, String rejectReason) {
        return jsonResult(costService.rejectCost(id, rejectReason, "设计工时确认单"));
    }

    /**
     * 撤销
     */
//    @ResponseBody
//    @PostMapping("/cancel")
//    //@RequiresPermissions({"cost:Deshouexa:success"})
//    public JsonResult cancelCost(Integer id) {
//        return jsonResult(costService.cancelCost(id, "设计工时确认单"));
//    }

}
